home *** CD-ROM | disk | FTP | other *** search
/ Internet.Works 40 / Issue 40.iso / pc / PCSoftware / DHTML Menu Builder 2.6 / dhtmlmen.exe / %AppDir% / AddIns / Context Menus.ext < prev    next >
Encoding:
Text File  |  2000-10-26  |  17.0 KB  |  596 lines

  1. If you compile the menus with this AddIn, a predefined menu will be displayed when the user rightclicks over the document, instead of browser's default context menu.
  2.  
  3. NOTE: This AddIn may not work with frames support turned on.
  4.  
  5. Developed by Xavier Flix
  6. ***
  7. //COPYRIGHT
  8.     
  9.     var nStyle = new Array;
  10.     var hStyle = new Array;
  11.     var nLayer = new Array;
  12.     var hLayer = new Array;
  13.     var nTCode = new Array;
  14.     
  15.     var AnimStep = 0;
  16.     var AnimHnd = 0;
  17.     var HTHnd = new Array;
  18.     var DoFormsTweak = false;
  19.     
  20.     var mFrame;
  21.     var cFrame;
  22.     
  23.     var OpenMenus = new Array;
  24.     var SelCommand;
  25.     var nOM = 0;
  26.     
  27.     var mX;
  28.     var mY;
  29.     var xOff = 0;
  30.     
  31.     var HideSpeed = 200;
  32.     
  33. //BROWSERDETECTION
  34.     
  35.     if ((frames.length==0) && IsMac)
  36.     frames.top = window;
  37.     
  38.     if(IE)
  39.     xOff = 2;
  40. //CODEFRAME
  41.     
  42. //FX
  43.     
  44. //STYLECODE
  45.  
  46.     function GetCurCmd() {
  47.         //IE
  48.         //This function will return the current command under the mouse pointer. It will return null if the mouse is not over any command.
  49.         //------------------------------
  50.         //Version 1.3
  51.         //
  52.         var cc = mFrame.window.event.srcElement;
  53.         while((cc.id=="") && (cc.tagName!="TD")) {
  54.             cc = cc.parentElement;
  55.             if(cc==null)
  56.                 break;
  57.         }
  58.         return cc;
  59.     }
  60.  
  61.     function HoverSel(mode, imgLName, imgRName) {
  62.         //IE
  63.         //This is the function called every time the mouse pointer is moved over or away from a command.
  64.         //------------------------------
  65.         //mode: 0 if the mouse is moving over the command and 1 if is moving away
  66.         //imgName: Name of the image object, if any, used in the selected command
  67.         //------------------------------
  68.         //Version 8.0
  69.         //
  70.         var imgL = new Image;
  71.         var imgLRsc = new Image;
  72.         var imgR = new Image;
  73.         var imgRRsc = new Image;
  74.         var mc;
  75.         
  76.         if(mode==0 && OpenMenus[nOM].SelCommand!=null)
  77.             HoverSel(1);
  78.         
  79.         if(imgLName!="_")
  80.             var imgL = eval("mFrame.document.images['"+imgLName+"']");
  81.         if(imgRName!="_")
  82.             var imgR = eval("mFrame.document.images['"+imgRName+"']");
  83.         
  84.         if(mode==0) {
  85.             mc = GetCurCmd();
  86.             if(nOM>1)
  87.                 if(mc==OpenMenus[nOM-1].SelCommand)
  88.                     return false;
  89.             if(OpenMenus[nOM].SelCommand || nOM>1)
  90.                 while(!InMenu())
  91.                     Hide();
  92.             mc.style.cssText = hStyle[mc.id];
  93.             if(imgLName!='_') imgLRsc = eval(imgLName+"On");
  94.             if(imgRName!='_') imgRRsc = eval(imgRName+"On");
  95.             OpenMenus[nOM].SelCommand = mc;
  96.             OpenMenus[nOM].SelCommandPar = [imgLName,imgRName];
  97.         } else {
  98.             mc = (mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
  99.             imgLName = (mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
  100.             imgRName = (mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
  101.             mc.style.background = "";
  102.             mc.style.cssText = nStyle[mc.id];
  103.             if(imgLName!='_') imgLRsc = eval(imgLName+"Off");
  104.             if(imgRName!='_') imgRRsc = eval(imgRName+"Off");
  105.             window.status = "";
  106.             OpenMenus[nOM].SelCommand = null;            
  107.         }
  108.         
  109.         if(imgLName!='_') {
  110.             imgL = eval("mFrame.document.images."+imgLName);
  111.             imgL.src = imgLRsc.src;
  112.         }
  113.         if(imgRName!='_') {
  114.             imgR = eval("mFrame.document.images."+imgRName);
  115.             imgR.src = imgRRsc.src;
  116.         }
  117.         
  118.         return true;
  119.     }
  120.  
  121.     function NSHoverSel(mode, mc, bcolor, w, h) {
  122.         //NS
  123.         //This is the function called every time the mouse pointer is moved over or away from a command.
  124.         //------------------------------
  125.         //mc: Name of the layer that corresponds to the selected command
  126.         //mode: 0 if the mouse is moving over the command and 1 if is moving away
  127.         //n: Unique ID that identifies this command. Used to retrieve the data from the nLayer or hLayer array.
  128.         //bcolor: Background color of the command. Ignored if the group uses a background image.
  129.         //w: Width of the command's layer.
  130.         //h: Height of the command's layer.
  131.         //------------------------------
  132.         //Version 10.0
  133.         //
  134.         var n;
  135.         var LayerHTM;
  136.         if(mode==0 && OpenMenus[nOM].SelCommand!=null)
  137.             NSHoverSel(1);
  138.         
  139.         if(mode==0) {
  140.             n = CBparseInt(mc.name.substr(2));
  141.             if(nOM>1)
  142.                 if(mc==OpenMenus[nOM-1].SelCommand)
  143.                     return;
  144.             while(!InMenu())
  145.                 Hide();
  146.             LayerHTM = hLayer[n];
  147.             OpenMenus[nOM].SelCommand = mc;
  148.             OpenMenus[nOM].SelCommandPar = [mc.bgColor,w,h];
  149.             mc.bgColor = bcolor;
  150.         } else {
  151.             mc = (mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
  152.             bcolor = (mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
  153.             w = (mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
  154.             h = (mode==1)?OpenMenus[nOM].SelCommandPar[2]:OpenMenus[nOM].OpenerPar[2];
  155.             n = CBparseInt(mc.name.substr(2));
  156.             LayerHTM = nLayer[n];
  157.             if(mc.parentLayer.background.src!="")
  158.                 mc.bgColor = null;
  159.             else
  160.                 mc.bgColor = bcolor;
  161.             window.status = "";
  162.             OpenMenus[nOM].SelCommand = null;
  163.         }
  164.         mc.resizeTo(w,h);
  165.         mc.document.open();
  166.         mc.document.write(LayerHTM);
  167.         mc.document.close();
  168.     }
  169.  
  170.     function Hide() {
  171.         //IE,NS
  172.         //This function hides the group identified by the menuId parameter.
  173.         //It also resets the values of the CurMenu and ParentMenu variables to reflect the changes of the hidden menu.
  174.         //------------------------------
  175.         //menuId: Name of the group's <div> or <layer> to be hidden.
  176.         //------------------------------
  177.         //Version 2.1
  178.         //
  179.         
  180.         if(AnimHnd)
  181.             window.clearTimeout(AnimHnd);
  182.         
  183.         if(OpenMenus[nOM].SelCommand!=null) {
  184.             if(IE) HoverSel(1);
  185.             if(NS) NSHoverSel(1);
  186.         }
  187.         if(OpenMenus[nOM].Opener!=null) {
  188.             if(IE) HoverSel(3);
  189.             if(NS) NSHoverSel(3);
  190.         }
  191.         
  192.         OpenMenus[nOM].visibility = "hidden";
  193.         window.clearTimeout(HTHnd[nOM]);
  194.         HTHnd[nOM] = 0;
  195.         nOM--;
  196.         
  197.         if(nOM>0)
  198.             if(!InMenu())
  199.                 HTHnd[nOM] = window.setTimeout("Hide()", HideSpeed);
  200.         
  201.         if(nOM==0)
  202.             FormsTweak("visible");
  203.     }
  204.  
  205.     function ShowMenu(mName, x, y, isCascading) {
  206.         //IE,NS
  207.         //This is the main function to show the menus when a hotspot is triggered or a cascading command is activated.
  208.         //------------------------------
  209.         //menuId: Name of the <div> or <layer> to be shown.
  210.         //x: Left position of the menu.
  211.         //y: Top position of the menu.
  212.         //isCascading: True if the menu has been triggered from a command, and not from a hotspot.
  213.         //------------------------------
  214.         //Version 12.9
  215.         //        
  216.         //(as) {
  217.         //indow.clearTimeout(as);
  218.         //s = 0;
  219.         //
  220.         x = CBparseInt(x);
  221.         y = CBparseInt(y);
  222.         if(AnimHnd && nOM>0) {
  223.             AnimStep=100;
  224.             Animate();
  225.         }
  226.         if(IE)
  227.             var Menu = mFrame.document.all[mName];
  228.         if(NS)
  229.             var Menu = mFrame.document.layers[mName];
  230.         if(!Menu)
  231.             return false;
  232.         if(IE)
  233.             Menu = Menu.style;
  234.         if(Menu==OpenMenus[nOM] || HTHnd[nOM])
  235.             return false;
  236.         
  237.         Menu.Opener = nOM>0?OpenMenus[nOM].SelCommand:null;
  238.         Menu.OpenerPar = nOM>0?OpenMenus[nOM].SelCommandPar:null;
  239.         Menu.SelCommand = null;
  240.         
  241.         if(!isCascading)
  242.             HideAll();
  243.         
  244.         var pW = GetWidthHeight()[0] + GetLeftTop()[0];
  245.         var pH = GetWidthHeight()[1] + GetLeftTop()[1];
  246.         
  247.         if(IE) {
  248.             if(isCascading) {
  249.                 x = CBparseInt(OpenMenus[nOM].left) + CBparseInt(OpenMenus[nOM].width) - 6;
  250.                 y = y + CBparseInt(OpenMenus[nOM].top) - 5;
  251.                 Menu.left = (x+CBparseInt(Menu.width)>pW)?CBparseInt(OpenMenus[nOM].left) - CBparseInt(Menu.width) + 6:x;
  252.                 Menu.top =  (y+CBparseInt(Menu.height)>pH)?pH - CBparseInt(Menu.height):y;
  253.             } else {
  254.                 Menu.left = (x+CBparseInt(Menu.width)>pW)?pW - CBparseInt(Menu.width):x;
  255.                 Menu.top =  (y+CBparseInt(Menu.height)>pH)?pH - CBparseInt(Menu.height):y;
  256.             }
  257.             if(!IsMac)
  258.                 Menu.clip = "rect(0 0 0 0)";
  259.         }
  260.         if(NS) {
  261.             if(isCascading) {
  262.                 x = OpenMenus[nOM].left + OpenMenus[nOM].clip.width - 6;
  263.                 y = OpenMenus[nOM].top + OpenMenus[nOM].SelCommand.top;
  264.                 x = (x+Menu.w>pW)?OpenMenus[nOM].left - Menu.w + 6:x;
  265.                 y = (y+Menu.h>pH)?pH - Menu.h:y;
  266.             } else {
  267.                 x = (x+Menu.w>pW)?pW - Menu.w:x;
  268.                 y = (y+Menu.h>pH)?pH - Menu.h:y;
  269.             }
  270.             Menu.clip.width = 0;
  271.             Menu.clip.height = 0;
  272.             Menu.moveToAbsolute(x,y);
  273.         }
  274.         if(isCascading)
  275.             Menu.zIndex = CBparseInt(OpenMenus[nOM].zIndex) + 1;
  276.         Menu.visibility = "visible";
  277.         OpenMenus[++nOM] = Menu;
  278.         HTHnd[nOM] = 0;
  279.         if((IE && !IsMac) || NS)
  280.             AnimHnd = window.setTimeout("Animate()", 10);
  281.         FormsTweak("hidden");
  282.         
  283.         return true;
  284.     }
  285.  
  286.     function Animate() {
  287.         //IE,NS
  288.         //This function is called by ShowMenu every time a new group must be displayed and produces the predefined unfolding effect.
  289.         //Currently is disabled for Navigator, because of some weird bugs we found with the clip property of the layers.
  290.         //------------------------------
  291.         //menuId: Name of the <div> or <layer> to be animated.
  292.         //------------------------------
  293.         //Version 1.3
  294.         //
  295.         var r = '';
  296.         var nw = nh = 0;
  297.         switch(fx) {
  298.             case 1:
  299.                 if(IE) r = "0 " + AnimStep + "% " + AnimStep + "% 0";
  300.                 if(NS) nw = AnimStep; nh = AnimStep;
  301.                 break;
  302.             case 2:
  303.                 if(IE) r = "0 100% " + AnimStep + "% 0";
  304.                 if(NS) nw = 100; nh = AnimStep;
  305.                 break;
  306.             case 3:
  307.                 if(IE) r = "0 " + AnimStep + "% 100% 0";
  308.                 if(NS) nw = AnimStep; nh = 100;
  309.                 break;
  310.             case 0:
  311.                 if(IE) r = "0 100% 100% 0";
  312.                 if(NS) nw = 100; nh = 100;
  313.                 break;
  314.         }
  315.         with(OpenMenus[nOM]) {
  316.             if(IE)
  317.                 clip =  "rect(" + r + ")";
  318.             if(NS) {
  319.                 clip.width = w*(nw/100);
  320.                 clip.height = h*(nh/100);
  321.             }
  322.         }
  323.         AnimStep += 20;
  324.         if(AnimStep<=100)
  325.             AnimHnd = window.setTimeout("Animate()",25);
  326.         else {
  327.             window.clearTimeout(AnimHnd);
  328.             AnimStep = 0;
  329.             AnimHnd = 0;
  330.         }
  331.     }
  332.  
  333.     function InMenu() {
  334.         //IE,NS
  335.         //This function returns true if the mouse pointer is over a group.
  336.         //------------------------------
  337.         //mX: Current X position of the mouse pointer.
  338.         //mY: Current Y position of the mouse pointer.
  339.         //m: <div> or <layer> object to be tested.
  340.         //------------------------------
  341.         //Version 1.2
  342.         //
  343.         var m = OpenMenus[nOM];
  344.         if(!m)
  345.             return false;
  346.         if(IE&&BV==4)
  347.             SetPointerPos();
  348.         var l = CBparseInt(m.left) + xOff;
  349.         var r = l+((IE)?CBparseInt(m.width):m.clip.width) - xOff;
  350.         var t = CBparseInt(m.top) + xOff;
  351.         var b = t+((IE)?CBparseInt(m.height):m.clip.height) - xOff;
  352.         return ((mX>=l && mX<=r) && (mY>=t && mY<=b));
  353.     }
  354.  
  355.     function SetPointerPos(e) {
  356.         //IE,NS
  357.         //This function sets the mX and mY variables with the current position of the mouse pointer.
  358.         //------------------------------
  359.         //e: Only used under Navigator, corresponds to the Event object.
  360.         //------------------------------
  361.         //Version 1.0
  362.         //
  363.         if(IE) {
  364.             if(event==null)
  365.                 if(mFrame.window.event==null)
  366.                     return;
  367.                 else
  368.                     e = mFrame.window.event;
  369.             else
  370.                 e = event;
  371.             mX = e.clientX + mFrame.document.body.scrollLeft;
  372.             mY = e.clientY + mFrame.document.body.scrollTop;
  373.         }
  374.         if(NS) {
  375.             mX = e.pageX;
  376.             mY = e.pageY;
  377.         }
  378.     }
  379.  
  380.     function HideMenus(e) {
  381.         //IE,NS
  382.         //This function checks if the mouse pointer is on a valid position and if the current menu should be kept visible.
  383.         //The function is called every time the mouse pointer is moved over the document area.
  384.         //------------------------------
  385.         //e: Only used under Navigator, corresponds to the Event object.
  386.         //------------------------------
  387.         //Version 24.0
  388.         //
  389.         SetPointerPos(e);
  390.         if(nOM>0)
  391.             if(OpenMenus[nOM].SelCommand!=null)
  392.                 while(!InMenu() && !HTHnd[nOM]) {
  393.                     HTHnd[nOM] = window.setTimeout("Hide()", HideSpeed);
  394.                     if(nOM==0)
  395.                         break;
  396.                 }
  397.     }
  398.  
  399.     function FormsTweak(state) {
  400.         //IE
  401.         //This is an undocumented function, which can be used to hide every form element on a page.
  402.         //This can be useful if the menus will be displayed over an area where is a combo box, which is an element that cannot be placed behind the menus and it will always appear over the menus resulting in a very undesirable effect.
  403.         //------------------------------
  404.         //Version 1.0
  405.         //
  406.         if(DoFormsTweak && IE)
  407.             for(var f = 0; f <= (mFrame.document.forms.length - 1); f++)
  408.                 for(var e = 0; e <= (mFrame.document.forms[f].elements.length - 1); e++)
  409.                     if(mFrame.document.forms[f].elements[e].type=="select-one")
  410.                         mFrame.document.forms[f].elements[e].style.visibility = state;
  411.     }
  412.  
  413.     function execURL(url, tframe) {
  414.         //IE,NS
  415.         //This function is called every time a command is triggered to jump to another page or execute some javascript code.
  416.         //------------------------------
  417.         //url: Encrypted URL that must be opened or executed.
  418.         //tframe: If the url is a document location, tframe is the target frame where this document will be opened.
  419.         //------------------------------
  420.         //Version 1.0
  421.         //
  422.         HideAll();
  423.         window.setTimeout("execURL2('" + url + "', '" + tframe + "')", 100);
  424.     }
  425.  
  426.     function execURL2(url, tframe) {
  427.         //IE,NS
  428.         //This function is called every time a command is triggered to jump to another page or execute some javascript code.
  429.         //------------------------------
  430.         //url: Encrypted URL that must be opened or executed.
  431.         //tframe: If the url is a document location, tframe is the target frame where this document will be opened.
  432.         //------------------------------
  433.         //Version 1.0
  434.         //
  435.         tframe = rStr(tframe);
  436.         var fObj = eval(tframe);
  437.         url = rStr(url);
  438.         if(url.indexOf("javascript")!=url.indexOf("vbscript"))
  439.             eval(url);
  440.         else
  441.             fObj.location.href = url;
  442.     }
  443.  
  444.     function rStr(s) {
  445.         //IE,NS
  446.         //This function is used to decrypt the URL parameter from the triggered command.
  447.         //------------------------------
  448.         //Version 1.1
  449.         //
  450.         s = xrep(s,"\x1E","'");
  451.         s = xrep(s,"\x1D","\x22");
  452.         s = xrep(s,"\x1C",",");
  453.         return s;
  454.     }
  455.  
  456.     function xrep(s, f, n) {
  457.         //IE,NS
  458.         //This function looks for any occurrence of the f string and replaces it with the n string.
  459.         //------------------------------
  460.         //Version 1.0
  461.         //
  462.         var tmp = s.split(f);
  463.         return tmp.join(n);
  464.     }
  465.  
  466.     function hNSCClick(e) {
  467.         //NS
  468.         //This function executes the selected command's trigger code.
  469.         //------------------------------
  470.         //Version 1.0
  471.         //
  472.         eval(this.TCode);
  473.     }
  474.     
  475.     function CBparseInt(n) {
  476.         //NS
  477.         //his function fixes a bug in Navigator's parseInt() function for the Mac.
  478.         //-----------------------------
  479.         //ersion 1.0
  480.         //
  481.         return IsMac?n:parseInt(n);
  482.     }
  483.  
  484.     function HideAll() {
  485.         //IE,NS
  486.         //This function will hide all the currently opened menus.
  487.         //------------------------------
  488.         //Version 1.0
  489.         //
  490.         while(nOM>0)
  491.             Hide();
  492.     }
  493.  
  494.     function GetLeftTop() {
  495.         //IE,NS
  496.         //This function returns the scroll bars position on the menus frame.
  497.         //------------------------------
  498.         //Version 1.0
  499.         //
  500.         if(IE)
  501.             return [mFrame.document.body.scrollLeft,mFrame.document.body.scrollTop];
  502.         if(NS)
  503.             return [mFrame.pageXOffset,mFrame.pageYOffset];
  504.     }
  505.  
  506.     function GetWidthHeight() {
  507.         //IE,NS
  508.         //This function returns the width and height of the menus frame.
  509.         //------------------------------
  510.         //Version 1.0
  511.         //
  512.         if(IE)
  513.             return [mFrame.document.body.clientWidth,mFrame.document.body.clientHeight];
  514.         if(NS)
  515.             return [mFrame.innerWidth,mFrame.innerHeight];
  516.     }
  517.  
  518.     function SetUpEvents() {
  519.         //IE,NS
  520.         //This function initializes the frame variables and setups the event handling.
  521.         //------------------------------
  522.         //Version 1.0
  523.         //
  524.         onerror = function(){;};
  525.         if(typeof(mFrame)=="undefined")
  526.         //MENUFRAME
  527.         if(typeof(mFrame)=="undefined")
  528.             window.setTimeout("SetUpEvents()",10);
  529.         else {
  530.             if(NS) {
  531.                 mFrame.captureEvents(Event.MOUSEMOVE);
  532.                 mFrame.onmousemove = HideMenus;
  533.                 mFrame.window.captureEvents(Event.MOUSEDOWN);
  534.                 mFrame.window.onmousedown = ShowContextMenu;
  535.                 PrepareEvents();
  536.             
  537.             }
  538.             
  539.             if(IE) {
  540.                 mFrame.document.onmousemove = HideMenus;
  541.                 mFrame.document.oncontextmenu = ShowContextMenu;
  542.             }
  543.         }
  544.     }
  545.  
  546.     function PrepareEvents() {
  547.         //NS
  548.         //This function is called right after the menus are rendered.
  549.         //It has been designed to attach the OnClick event to the <layer> tag. This is being
  550.         //done this way because Navigator does not support inline event capturing on the
  551.         //click event in the layer tag... duh!
  552.         //------------------------------
  553.         //Version 2.0
  554.         //
  555.         for(var l=0; l<mFrame.document.layers.length; l++) {
  556.             var lo = mFrame.document.layers[l];
  557.             lo.w = lo.clip.width;
  558.             lo.h = lo.clip.height;
  559.             for(var sl=0; sl<lo.layers.length; sl++) {
  560.                 var slo = mFrame.document.layers[l].layers[sl];
  561.                 if(slo.name.indexOf("EH")>0) {
  562.                     slo.document.captureEvents(Event.CLICK);
  563.                     slo.document.onclick = hNSCClick;
  564.                     slo.document.TCode = nTCode[slo.name.split("EH")[1]];
  565.                 }                    
  566.             }
  567.         }
  568.     }
  569.  
  570.     function ShowContextMenu(e) {
  571.         //IE,NS
  572.         //This function is called when a user rightclicks on the document and it will show a predefined menu.
  573.         //
  574.         //Just change the value of this variable to the name of the group
  575.         //you want to display when the user rightclicks on the document.
  576.         var MenuName = 'mnuNav';
  577.         
  578.         if(IE)
  579.             if(!document.all[MenuName])
  580.                 alert("The Context Menus AddIn is not correctly configured");
  581.             else                
  582.                 ShowMenu(MenuName, event.x + GetLeftTop()[0], event.y + GetLeftTop()[1], false);
  583.         
  584.         if(NS)
  585.             if(e.which==3)
  586.                 if(!document.layers[MenuName])
  587.                     alert("The Context Menus AddIn is not correctly configured");
  588.                 else                
  589.                     ShowMenu(MenuName, e.x, e.y, false);
  590.         
  591.         return false;
  592.     }
  593.  
  594. //BROWSERCODE
  595. //EXPAND
  596.